import socket

# 创建一个 TCP/IP 套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定套接字到地址和端口
server_address = ('localhost', 10000)
sock.bind(server_address)

# 监听传入连接
sock.listen(1)

while True:
    # 等待连接
    print('等待连接...')
    connection, client_address = sock.accept()

    try:
        print('连接来自:', client_address)

        # 接收数据
        received_bytes = connection.recv(1024)

        # 将字节数组转换成字符串
        received_string = received_bytes.decode('utf-8')

        # 输出转换后的字符串
        print('收到的消息:', received_string)
    except:
        print('连接异常')
    else:
        print('没有异常，其他处理')
    finally:
        # 清理连接
        connection.close()